package com.loup.configure;

import com.loup.executor.Executor;
import com.loup.executor.LoupExecutor;
import com.loup.handler.SimpleTypeHandler;
import com.loup.handler.TypeHandler;
import com.loup.registory.MapperRegistory;
import com.loup.registory.MethodSqlMapper;
import com.loup.session.DefaultLoupSqlSession;
import com.loup.session.LoupSqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by loup on 2018/4/26.
 */
public class LoupConfiguration {

    private static String url;
    private static String className;
    private static String userName;
    private static String password;


    private List<MethodSqlMapper> methodSqlMappers = new ArrayList<MethodSqlMapper>();

    private MapperRegistory mapperRegistories = new MapperRegistory();

    private TypeHandler typeHandler = new SimpleTypeHandler();

    public LoupConfiguration() {

    }

    public List<MethodSqlMapper> getMethodSqlMappers() {
        return methodSqlMappers;
    }

    public MapperRegistory getMapperRegistories() {
        return mapperRegistories;
    }

    public TypeHandler getTypeHandler() {
        return typeHandler;
    }

    public void build() {
        if (methodSqlMappers != null && methodSqlMappers.size() > 0) {
            for (MethodSqlMapper methodSqlMapper: methodSqlMappers) {
                mapperRegistories.addMapper(methodSqlMapper.getMethodPath(), methodSqlMapper.getSql(), methodSqlMapper.getReturnType());
            }
        }
    }

    public LoupSqlSession getSqlSession() {
        return new DefaultLoupSqlSession(this, getExecutor());
    }

    public Executor getExecutor() {
        return new LoupExecutor();
    }

    public static String getUrl() {
        return url;
    }

    public static void setUrl(String url) {
        LoupConfiguration.url = url;
    }

    public static String getClassName() {
        return className;
    }

    public static void setClassName(String className) {
        LoupConfiguration.className = className;
    }

    public static String getUserName() {
        return userName;
    }

    public static void setUserName(String userName) {
        LoupConfiguration.userName = userName;
    }

    public static String getPassword() {
        return password;
    }

    public static void setPassword(String password) {
        LoupConfiguration.password = password;
    }
}
